﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var TF = "select * from Students";
            var dt = DbHelper.GetDataTable(TF);
            dataGridView1.DataSource = dt;


            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.ReadOnly = true;
            dataGridView1.AllowUserToAddRows = false;
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;
            var sql = string.Format("select * from Students where SName like '%{0}%'" ,name);
            var dt = DbHelper.GetDataTable(sql);
            dataGridView1.DataSource = dt;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            EditForm editForm = new EditForm();
            var res = editForm.ShowDialog();
            if (res == DialogResult.Yes)
            {
               // MessageBox.Show("YES");
                var StuSql = "select* from Students";
                var dt = DbHelper.GetDataTable(StuSql);
                dataGridView1.DataSource = dt;
            }
            else
            {
                MessageBox.Show("未添加任何信息","提示");
            }
           // EditForm form = new EditForm();
           // form.Show();

        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            var Id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
            var SName = (string)dataGridView1.SelectedRows[0].Cells["SName"].Value;
            var Age = (int)dataGridView1.SelectedRows[0].Cells["Age"].Value;
            var Score = (int)dataGridView1.SelectedRows[0].Cells["Score"].Value;
            EditForm form = new EditForm(Id,SName,Age,Score);
            var res = form.ShowDialog();
            if (res == DialogResult.Yes)
            {
                var StuSql = "select* from Students";
                var dt = DbHelper.GetDataTable(StuSql);
                dataGridView1.DataSource = dt;

            }
            else
            {
                MessageBox.Show("NO");
            }
           
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            var row=dataGridView1.SelectedRows[0];
            var cell = row.Cells[0];
            var Id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
            var sql = string.Format("delete from Students where Id={0}", Id);
            var res = DbHelper.AddOrUpdateOrDelete(sql);
            if (res == 1)
            {
                MessageBox.Show("删除成功！", "提示");

                var StuSql = "select* from Students";
                var dt = DbHelper.GetDataTable(StuSql);
                dataGridView1.DataSource = dt;
            }
            else 
            {
                MessageBox.Show("删除失败！", "提示");
            }


        }
    }
}
